//
//  LRXuserView.m
//  路人侠
//
//  Created by zhangxulong on 2/14/16.
//  Copyright © 2016 zhangxulong. All rights reserved.
//

#import "LRXuserView.h"

#import "zxlCircleView.h"
@interface LRXuserView()


@property (nonatomic,strong) UIImageView * userImage;
@property (nonatomic,strong) UILabel * userName;
@property (nonatomic,strong) zxlCircleView * circleView;
@end

@implementation LRXuserView
- (UIImageView *)userImage
{
    if (_userImage == nil) {
        _userImage = [[UIImageView alloc]init];
    }
    return _userImage;
}

- (UILabel *)userName
{
    if (_userName == nil) {
        _userName = [[UILabel alloc]init];
    }
    return _userName;
}
- (zxlCircleView *)circleView{
    if (_circleView == nil) {
        _circleView = [[zxlCircleView alloc]init];
    }
    return _circleView;
}

+ (instancetype)layoutUserView:(LRXUserModel *)userModel imageViewSize:(CGSize)imageViewSize
{
    LRXuserView * userView = [[LRXuserView alloc] initWithModel:userModel imageViewSize:imageViewSize];
    return userView;
}

- (instancetype)initWithModel:(LRXUserModel *)userModel imageViewSize:(CGSize)imageViewSize
{
    if (self = [super init]) {
        
        CGFloat screenW = imageViewSize.width;
        CGFloat userImageH = imageViewSize.height;
        
        self.circleView.imageName = userModel.userImage;
    
        self.circleView.frame = CGRectMake(0, 0, screenW, userImageH);
        self.circleView.backgroundColor = [UIColor clearColor];
        self.circleView.borderColor = [UIColor clearColor];
        self.circleView.lineWidth = 0;

        
        //设置 用户姓名
        self.userName.text = userModel.userName;
        self.userName.font = [UIFont systemFontOfSize:20 * hScale];
        [self.userName sizeToFit];
        //计算出最大的宽度
        self.maxW = MAX(imageViewSize.width, self.userName.frame.size.width);
        
        //先确定 userImage 的中心点 ，再根据userImage的中心点 确定 userName的中心点
        self.circleView.center = CGPointMake(self.maxW * 0.5, self.circleView.center.y);
        //应该先 sizeToFit 就可以 判断 UILabel 的大小 再设置中心点
        CGPoint point = self.circleView.center;
        CGFloat pointY = userImageH * 0.5 + 14 * hScale;
        point.y += pointY;
        self.userName.center = point;
        
        [self addSubview:self.circleView];
        //[self addSubview:self.userName];
    }
    
    return self;
}
@end
